﻿@using M2B.GestionEntretiens.Helpers.HTML
@using M2B.GestionEntretiens.POCOS
@model M2B.GestionEntretiens.Models.CandidatsModel

@{
    ViewBag.Title = "Candidats";
    Layout = "~/Views/Shared/_Layout.cshtml";
}


<script type="text/javascript">
    var paysCandidat = JSON.parse('@Html.Raw(Json.Encode((List<Referentiel>)ViewData["PaysCandidat"]))');
    var statutCandidat = JSON.parse('@Html.Raw(Json.Encode((List<Referentiel>)ViewData["StatutCandidat"]))');
    var formationCandidat = JSON.parse('@Html.Raw(Json.Encode((List<Referentiel>)ViewData["FormationCandidat"]))');
    var profilCandidat = JSON.parse('@Html.Raw(Json.Encode((List<Referentiel>)ViewData["ProfilCandidat"]))');
    var langueCandidat = JSON.parse('@Html.Raw(Json.Encode((List<Referentiel>)ViewData["LangueCandidat"]))');
</script>

<script type="text/javascript">
    var candidatId = 0;
    //La liste des candidats
    var candidates;
    $(document).ready(function () {

        LoadCandidats();

    });
    //Chargement des candidats
    function LoadCandidats() {

        $.ajax({
            url: '@Url.Action("LoadCandidats","Candidats")',
            type: 'GET',
            cache: false,
            success: function (result) {
              
                candidates = JSON.parse(result);

                LoadGridCandidats();


            }
        });

    }

    function LoadGridCandidats() {

        jQuery("#CandidatsTable").jqGrid({
            datatype: "jsonstring",
            datastr: candidates,
            colNames: ["<img style='height:16px;width:16px;' src='../Content/Images/Ajouter.png' onclick='AjouterCandidat()'>",'Id', 'Nom', 'Prenom', 'Statut', 'Identifiant', 'Pays', 'Langue', 'Expérience', 'Formation', 'Profil', 'Mail', 'Télephone', 'Adresse'],
            colModel: [
                { name: 'act', index: 'act', width: 65, sortable: false, search: false },
                { name: 'CandidatId', index: 'CandidatId', width: 40, sorttype: "int", key: true, hidden: true },
                { name: 'Nom', index: 'Nom', width: 200, sorttype: "string" },
                { name: 'Prenom', index: 'Prenom', width: 200, sorttype: "string" },
                {
                    name: 'Statut',
                    index: 'Statut',
                    width: 100,
                    sorttype: "string",
                    align: "left",
                    formatter: function (Statut) {

                        var result = "";
                        for (var i = 0; i < statutCandidat.length; i++) {

                            if (statutCandidat[i].Code == Statut) {
                                result = statutCandidat[i].Valeur;
                                break;
                            }
                        }

                        return result;
                    }
                },
                { name: 'NCIN', index: 'NCIN', width: 120, sorttype: "string" },


                {
                    name: 'Pays',
                    index: 'Pays',
                    width: 100,
                    sorttype: "string",
                    align: "left",
                    formatter: function (Pays) {

                        var result = "";
                        for (var i = 0; i < paysCandidat.length; i++) {

                            if (paysCandidat[i].Code == Pays) {
                                result = paysCandidat[i].Valeur;
                                break;
                            }
                        }

                        return result;
                    }
                },
                {
                    name: 'Langue',
                    index: 'Langue',
                    width: 100,
                    sorttype: "string",
                    align: "left",
                    formatter: function (Langue) {

                        var result = "";
                        for (var i = 0; i < langueCandidat.length; i++) {

                            if (langueCandidat[i].Code == Langue) {
                                result = langueCandidat[i].Valeur;
                                break;
                            }
                        }

                        return result;
                    }
                },
                { name: 'Experience', index: 'Experience', width: 120, sorttype: "string", align: "right", },
                {
                    name: 'Formation',
                    index: 'Formation',
                    width: 100,
                    sorttype: "string",
                    align: "left",
                    formatter: function (Formation) {

                        var result = "";
                        for (var i = 0; i < formationCandidat.length; i++) {

                            if (formationCandidat[i].Code == Formation) {
                                result = formationCandidat[i].Valeur;
                                break;
                            }
                        }

                        return result;
                    }
                },
                
             
                {
                    name: 'Profil',
                    index: 'Profil',
                    width: 200,
                    sorttype: "string",
                    align: "left",
                    formatter: function (Profil) {

                        var result = "";
                        for (var i = 0; i < profilCandidat.length; i++) {

                            if (profilCandidat[i].Code == Profil) {
                                result = profilCandidat[i].Valeur;
                                break;
                            }
                        }

                        return result;
                    }
                },
                { name: 'AdresseMail', index: 'AdresseMail', width: 150, sorttype: "string" },
                { name: 'Telephone', index: 'Telephone', width: 120, sorttype: "string" },
                { name: 'Adresse', index: 'Adresse', width: 150, sorttype: "string" }

            ],
            gridComplete: function () {
                var ids = jQuery("#CandidatsTable").jqGrid('getDataIDs');
                for (var i = 0; i < ids.length; i++) {
                    var data = ids[i];
                    var btnEdit = "<img style='height:16px;width:16px;margin-right:5px;' src='../Content/Images/Editer.png' onclick='EditCandidat(" + data + ")' />";
                    var btnSupprimer = "<img style='height:16px;width:16px;' src='../Content/Images/Supprimer.png' onclick='AfficherDeleteCandidatPopUp(" + data + ")' />";
                    jQuery("#CandidatsTable").jqGrid('setRowData', ids[i], { act: btnEdit + btnSupprimer });
                }
            },
            rowNum: 20,
            rowList: [10, 20, 30],
            pager: '#CandidatsPager',
            height: 480,
            sortname: 'Nom',
            width: 950,
            viewrecords: true,
            loadonce: true,
            sortorder: "asc",
            multiselect: false,
            caption: "Candidats"


        });



        jQuery("#CandidatsTable").jqGrid('navGrid', '#CandidatsPager', { edit: false, add: false, save: false, del: false });

        jQuery("#CandidatsTable").jqGrid('filterToolbar', { stringResult: true, searchOnEnter: false, defaultSearch: "cn" });

    }

</script>
@*La liste des méthodes*@
<script type="text/javascript">
    
    function EditCandidat(Id) {
        candidatId = Id;
        $.ajax({
            type: "GET",
            data: { candidatId: candidatId },
            url: '@Url.Action("EditCandidat")',
            success: function (result) {

                $("#dialogEditCandidat").html(result).dialog({ width: 800, position: ['center', 'top'], modal: true });


            }
        });
    }

    function AfficherDeleteCandidatPopUp(Id) {
       
        candidatId = Id;
        $("#CandidatsTable").setSelection(candidatId, false);
        $("#dialogDeleteCandidat").dialog({ width: 400, position: ['center', 'center'], modal: true });
    }

    function DeleteCandidat() {


        $.ajax({
            url: '@Url.Action("DeleteCandidat")',
            type: 'POST',
            data: { candidatId: candidatId }, success: function () {
                $('#CandidatsTable').delRowData(candidatId);
                $("#dialogDeleteCandidat").dialog("close");
            }
        });

    }

    function AjouterCandidat() {
        $.ajax({
            type: "GET",
            url: '@Url.Action("AddCandidat")',
            success: function (result) {

                $("#dialogAddCandidat").html(result).dialog({ width: 800, position: ['center', 'top'], modal: true });


            }
        });
    }
</script>



<table id="CandidatsTable"></table>
<div id="CandidatsPager">
</div>
<div id="dialogDeleteCandidat" title="Supprimer un candidat" style="display: none;">
    <label style="width: 350px"> Voulez-vous vraiment supprimer ce candidat ?</label>
    <p style="float: right">
        @Html.ActionImage("", "", "~/Content/Images/Valider.png", "Valider", "32px", "32px", "DeleteCandidat()")
        @Html.ActionImage("Index", "", "~/Content/Images/Annuler.png", "Annuler", "32px", "32px", "")
    </p>
</div>
<div id="dialogAddCandidat" title="Ajouter un candidat" style="display: none;">

</div>

<div id="dialogEditCandidat" title="Edition d'un candidat" style="display: none;">

</div>